package com.housekeeping.housekeeping.order.controller.order;

import com.housekeeping.housekeeping.order.entity.Order;

import com.housekeeping.housekeeping.order.utils.PageUtils;
import com.housekeeping.housekeeping.order.utils.ResponseCode;
import com.housekeeping.housekeeping.order.utils.ResponseEntity;
import com.housekeeping.housekeeping.order.utils.ResponseUtils;
import com.housekeeping.housekeeping.order.service.impl.OrderSearchServImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/order/stateAndPhone")
public class OrderSearchByStateAndPhone extends HttpServlet {
    private OrderSearchServImpl orderSearchServ = new OrderSearchServImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        int state = Integer.parseInt(req.getParameter("state"));
        //分页需要的两个参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        ResponseEntity<PageUtils<List<Order>>> entity = null;

        try {
            if (phone == null || phone.equals("")) {
                PageUtils<List<Order>> stateList = orderSearchServ.getStateList(state, page, pageSize);
                entity = ResponseEntity.success(stateList);
            } else if (phone != null && !phone.equals("")) {
                PageUtils<List<Order>> pAndSList = orderSearchServ.getPAndSList(phone, state, page, pageSize);
                entity = ResponseEntity.success(pAndSList);
            } else {
                entity = ResponseEntity.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntity.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
